package com.lottery.bootdo.common.controller.finance;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lottery.bootdo.common.service.ActivityInfoService;
import com.lottery.bootdo.common.utils.PageUtils;
import com.lottery.bootdo.common.utils.StringUtils;
import com.lottery.common.contants.ReQueryResult;
import com.lottery.common.entity.EventsInfo;
import com.lottery.common.entity.OrderInfo;
import com.lottery.common.service.DecryptToObjectService;
import com.lottery.common.service.OrderInfoService;
import com.lottery.common.service.TextInfoService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.lang.util.NutMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;

@Controller
@RequestMapping("/income")
@Slf4j
@Api(value = "/income",tags="财务管理操作类")
public class IncomeController {
	private ReQueryResult result = new ReQueryResult();

	@Autowired
	private OrderInfoService orderInfoService;
	@Autowired
	private DecryptToObjectService decryptToObjectService;


	@RequiresPermissions("income:recharge:list")
	@RequestMapping(value = "/recharge", method = RequestMethod.GET)
	String index() {
		return "common/income/recharge/index";
	}

	@RequiresPermissions("income:expend:list")
	@RequestMapping(value = "/expend", method = RequestMethod.GET)
	String expend() {
		return "common/income/expend/index";
	}


	@RequestMapping(value = "", method = RequestMethod.GET)
	@RequiresPermissions(value={"income:recharge:list","income:expend:list"},logical= Logical.OR)
	@ResponseBody
	public PageUtils rechargeList(String json){
		log.info("__________ rechargeList");
		result = new ReQueryResult();
		NutMap nutMap=decryptToObjectService.jsonToObject(json,NutMap.class);
		log.info("nutmap is {}", JSON.toJSONString(nutMap));

		QueryWrapper<OrderInfo> queryWrapper=new QueryWrapper<>();
		if (StringUtils.isNotBlank(nutMap.getString("phone")))
			queryWrapper.eq("phone",nutMap.getInt("phone"));

		queryWrapper.in("pay_type","1","2").eq("status",0);
		Page<OrderInfo> page = new Page<>(nutMap.getInt("pageIndex"),nutMap.getInt("pageLength"));
		IPage<OrderInfo> records=orderInfoService.page(page,queryWrapper);
		log.info(JSON.toJSONString(records));
		PageUtils pageUtils = new PageUtils(records.getRecords(),new BigDecimal(records.getTotal()).intValue());
		return pageUtils;
	}

}
